(* Graphic Interface User *)

(*Main Window*)
let window = GWindow.window
    ~width:1024
    ~height:768
    ~resizable:true
    ~position:`CENTER
    ~title:"Cardinogen" ()


(* GtkMessageDialog - Court message à l'attention de l'utilisateur. *)
let confirm _ =
  let dlg = GWindow.message_dialog
    ~message:"<b><big>Would you really leave?</big>\n\n\
      Warning: \nAll changes will be lost.</b>\n"
    ~parent:window
    ~destroy_with_parent:true
    ~use_markup:true
    ~message_type:`QUESTION
    ~position:`CENTER_ON_PARENT
    ~buttons:GWindow.Buttons.yes_no () in
  let res = dlg#run () = `NO in
  dlg#destroy ();
  res



(* Function *)

let destroy () = GMain.Main.quit ()

(* Main Box *)
let vbox = GPack.vbox
               ~spacing:10
               ~border_width:10
               ~packing:window#add ()


(* Menu bar *)

let menubar = GMenu.menu_bar 
  ~packing:vbox#pack ()


let factory = new GMenu.factory menubar

let file_menu = factory#add_submenu "File"
let pref_menu = factory#add_submenu "Preference"
let help_menu = factory#add_submenu "Help"
let abou_menu = factory#add_submenu "About"


  (* File menu *)
let factory = new GMenu.factory file_menu
let sub_menu = factory#add_item "Save"
let sub_menu = factory#add_item "Save as"
let sub_menu = factory#add_item "Quit" ~callback:window#destroy




  (* Preference menu *)
let factory = new GMenu.factory pref_menu
let sub_menu = factory#add_item "Load Profile"

  (* Help menu *)
(* Under construction *)



  (* About menu *)
let factory = new GMenu.factory abou_menu
let sub_menu = factory#add_item "Us"
let sub_menu = factory#add_item "Cardinogen"


  (* Notebook menu *)
let notebook = GPack.notebook ~packing:vbox#add ()


  (* Note Load *)
let load_nb = 
  GBin.scrolled_window
    ~vpolicy:`AUTOMATIC
    ~hpolicy:`AUTOMATIC
    ~width:20
    ~packing:
    (fun w -> ignore (notebook#append_page
                        ~tab_label:(GMisc.label ~text:"Loading" ())#coerce w)) ()
let load_hbox = GPack.hbox 
  ~packing:load_nb#add_with_viewport ()

let load_image = GMisc.image ~packing:load_hbox#add ()


let load_bbox = GPack.button_box `VERTICAL
  ~spacing:20
  ~layout:`SPREAD
  ~packing:(load_hbox#pack ~expand:false) ()



  (* Note Process *)
let proce_nb = 
  GBin.scrolled_window
    ~vpolicy:`AUTOMATIC
    ~hpolicy:`AUTOMATIC
    ~width:20
    ~packing:
    (fun w -> ignore (notebook#append_page
                          ~tab_label:(GMisc.label ~text:"Processing" ())#coerce w)) ()

let proce_hbox = GPack.hbox 
  ~packing:proce_nb#add_with_viewport ()

let proce_image = GMisc.image ~packing:proce_hbox#add ()


let proce_bbox = GPack.button_box `VERTICAL
  ~spacing:20
  ~layout:`SPREAD
  ~packing:(proce_hbox#pack ~expand:false) ()


  (* Note Display *)
let disp_nb = 
  GBin.scrolled_window
    ~vpolicy:`AUTOMATIC
    ~hpolicy:`AUTOMATIC
    ~width:20
    ~packing:
    (fun w -> ignore (notebook#append_page
                        ~tab_label:(GMisc.label ~text:"3D Display" ())#coerce w)) ()

(* OpenGl *)
let hb = GPack.hbox ~packing:disp_nb#add_with_viewport ()
let area = GlGtk.area [`DOUBLEBUFFER;`RGBA;`DEPTH_SIZE 1]
  ~width:700 
  ~height:500 
  ~packing:hb#add ()


(*The Status Bar*)
let st_bar = GMisc.statusbar
               ~packing:(vbox#pack) ()
let ctxt = st_bar#new_context ~name:"Global"
let add_status s =
  ctxt#pop();
  ctxt#push s;
  ()


let path = ref ""

(*Set the map with the new image selected*)
let set_map_file file_sel () = 
  path := file_sel#filename;
  load_image#set_file !path;
  add_status ("Image: " ^ !path);
  file_sel#destroy(); ()


let map_wire () =
  if (!path <> "") then
    begin
     Image.draw_squared_grid !path 10;
     proce_image#set_file (!path^"_ww");
     add_status ("Wire: " ^ !path ^ "_ww");
     ()
    end
  else
    begin
     add_status "No Image Selected!!";
     ()  
    end

let map_cont () =
  if (!path <> "") then
    begin
     Image.detect_outlines !path;
     proce_image#set_file (!path^"_wc");
     add_status ("Contour: " ^ !path ^ "_wc");
     ()
    end
  else
    begin
     add_status "No Image Selected!!";
     ()  
    end

let map_wire_cont () =
  if (!path <> "") then
    begin
     Image.detect_outlines !path;
     Image.draw_squared_grid (!path^"_wc") 10;
     proce_image#set_file (!path^"_wc_ww");
     add_status ("Wire & Contour: " ^ !path ^ "__wc_ww");
     ()
    end
  else
    begin
     add_status "No Image Selected!!";
     ()  
    end

(* File manager *)
let file_mangr () =
  let file_sel = GWindow.file_selection 
                              ~title:"CardinoGen - Select an image"
			      ~position:`CENTER_ON_PARENT
                              ~filename:""
                              ~border_width:10 () in
    file_sel#cancel_button#connect#clicked ~callback:file_sel#destroy;
    file_sel#ok_button#connect#clicked ~callback:(set_map_file file_sel);
    file_sel#show ();
    ()

(* Buttons *)
let open_file =
  let button = GButton.button
                 ~label:"Open"
                 ~packing:load_bbox#add () in
    button#connect#clicked ~callback:file_mangr;
    button

let launch_cont =
  let button = GButton.button
                 ~label:"Contour"
                 ~packing:proce_bbox#add () in
    button#connect#clicked ~callback:map_cont;
    button

let launch_cont =
  let button = GButton.button
                 ~label:"Wire"
                 ~packing:proce_bbox#add () in
    button#connect#clicked ~callback:map_wire;
    button

let launch_cont =
  let button = GButton.button
                 ~label:"Contour & Wire"
                 ~packing:proce_bbox#add () in
    button#connect#clicked ~callback:map_wire_cont;
    button

(* "main" function*)
let run () =
  (*Show the Window*)
  window#event#connect#delete confirm;
  window#show ();
  GMain.Main.main ();
  ()

